/**
 *
 * The MIT License
 *
 * Copyright (c) 2009 Noritaka Horio All rights reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 * 
 * @author Noritaka Horio 
 * @copyright Copyright (c) 2009 Noritaka Horio All rights reserved.
 * @license The MIT License
 * @version $Rev: 186 $ $Date: 2009-11-07 12:32:26 +0000 (Sat, 07 Nov 2009) $
 * @see http://mootools.net
 * 
 */

var Control;
if (!Control) Control = {};

Control.PeriodicalTaskList = new Class({

	initialize: function(container) {
		this.container = $(container);
		this.table = new Element("table", {"class": "control"});
		this.table.inject(this.container);
		this.createHeader();
		this.intervals = ["10", "20", "30", "40", "50", "60"];
		this.rows = $(this.table).getElements("tr");
		this.header = this.rows.shift();
	},

	createHeader: function() {
		var row = new Element("tr");
		var cols = [
			new Element("th", {"html": "no."}),
			new Element("th", {"html": "時間"}),
			new Element("th", {"html": "タスク"}),
			new Element("th", {"html": ""})
		];
		row.adopt(cols);
		row.inject(this.table);
	},

	addRow: function(task) {
		var row = new Element("tr");
		row.inject(this.table);
		this.rows = $(this.table).getElements("tr");
		this.header = this.rows.shift();
		var cols = [
			new Element("td", {"html": this.rows.length, "class": "no"}),
			this.createTimeColumn(task.time),
			this.createPluginColumn(task.plugin),
			this.createDeleteColumn()
		];
		row.adopt(cols);
	},

	createTimeColumn: function(time) {
		var timeColumn = new Element("td");
		var a = new Element("a", {"html": time, "class": "time", "href": "#" + time.toString()});
		a.inject(timeColumn);
		a.addEvent("click", this.onTimeButtonClick.bind(this, [a]));
		return timeColumn;
	},

	createPluginColumn: function(plugin) {
		var pluginColumn = new Element("td");
		var a = new Element("a", {"html": plugin, "class": "plugin", "href": "#" + plugin});
		a.inject(pluginColumn);
		a.addEvent("click", this.onPluginButtonClick.bind(this, [a]));
		return pluginColumn;
	},

	createDeleteColumn: function() {
		var row = this.rows.getLast();
		var deleteColumn = new Element("td");
		var span = new Element("span", {"html": "削除", "class": "label"});
		var a    = new Element("a", {"class": "button"});
		span.inject(a);
		a.inject(deleteColumn);
		a.addEvent("click", this.onDeleteButtonClick.bind(this, [row]));
		return deleteColumn;
	},


	getRows: function() {
		var rows = new Array();
		this.rows.each(function(r,k){
			var time = r.getElement("a.time");
			var plugin = r.getElement("a.plugin");
			var row = {
				"time": time.get("html"),
				"plugin": plugin.get("html")
			};
			this.push(row);
		}.bind(rows));
		return rows;
	},


	onTimeButtonClick: function(element) {
		var properies	= WindowOptions.minutespicker;
		var size		= WindowSize.minutespicker;
		var plugin		= WindowFactory.createWindow(properies,size);
		var position 	= element.getPosition();

		var htmlLoader = new air.HTMLLoader();
		htmlLoader.height = size.height;
		htmlLoader.width = size.width;
		htmlLoader.paintsDefaultBackground = false;
		htmlLoader.visible = true;

		plugin.stage.addChild(htmlLoader);
		plugin.title = "minutespicker";

		plugin.x = position.x + nativeWindow.x;
		plugin.y = position.y + nativeWindow.y + 50;
		plugin.alwaysInFront  = true;

		htmlLoader.addEventListener(air.Event.COMPLETE, this.onLoadMinutesPickerComplete.bind({"loader": htmlLoader}, [element]));
		plugin.addEventListener(air.Event.CLOSE, this.onMinutesSelectComplete.bind({"loader": htmlLoader}, [element]));
		htmlLoader.load(new air.URLRequest("minutespicker.html"))

		plugin.activate();
	},

	onPluginButtonClick: function(element) {
		var properies	= WindowOptions.pluginpicker;
		var size		= WindowSize.pluginpicker;
		var plugin		= WindowFactory.createWindow(properies,size);
		var position 	= element.getPosition();

		var htmlLoader = new air.HTMLLoader();
		htmlLoader.height = size.height;
		htmlLoader.width = size.width;
		htmlLoader.paintsDefaultBackground = false;
		htmlLoader.visible = true;

		plugin.stage.addChild(htmlLoader);
		plugin.title = "pluginpicker";

		plugin.x = position.x + nativeWindow.x;
		plugin.y = position.y + nativeWindow.y + 50;
		plugin.alwaysInFront  = true;

		htmlLoader.addEventListener(air.Event.COMPLETE, this.onLoadPluginPickerComplete.bind({"loader": htmlLoader}, [element]));
		plugin.addEventListener(air.Event.CLOSE, this.onPluginSelectComplete.bind({"loader": htmlLoader}, [element]));
		htmlLoader.load(new air.URLRequest("pluginpicker.html"))

		plugin.activate();
	},

	onDeleteButtonClick: function(rowElement) {
		rowElement.dispose();
		this.rows = $(this.table).getElements("tr");
		this.header = this.rows.shift();
		this.rows.each(function(r,k){
			var no = $(r).getElement("td.no");
			no.set("html", k + 1);
		});
	},

	onLoadMinutesPickerComplete: function(element) {
		var value = element.get("html");
		var win = this.loader.window;
		if (win.MinutesPicker) {
			win.MinutesPicker.setMinutes(value);
		}
		this.loader.removeEventListener(air.Event.COMPLETE, arguments.callee);
	},

	onMinutesSelectComplete: function(element) {
		var win   = this.loader.window;
		var minutes = win.MinutesPicker.getMinutes();

		element.set("html", minutes);
	},










	onLoadPluginPickerComplete: function(element) {
		var value = element.get("html");
		var win = this.loader.window;
		if (win.PluginPicker) {
			win.PluginPicker.setSelectValue(value);
		}
		this.loader.removeEventListener(air.Event.COMPLETE, arguments.callee);
	},

	onPluginSelectComplete: function(element) {
		var win   = this.loader.window;
		var plugin = win.PluginPicker.getSelectValue();

		element.set("html", plugin.name);
	},

});